﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Data;
using System.Windows.Controls;
using System.Globalization;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Markup;
using System.Windows.Media;

//http://efreedom.com/Question/1-2188165/Canceling-TextBox-Input-Validation-Error-WPF
namespace hasselblad
{
    public class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }




        // This is the actual double data property for the top slider and text block
        double doubleProperty;
        public double DoubleProperty
        {
            get { return doubleProperty; }
            set
            {
                if (value != doubleProperty)
                {
                    doubleProperty = value;
                    NotifyPropertyChanged("DoubleProperty");
                }
            }
        }


        //// This is the actual double data property for the bottom slider and text block
        //double doubleProperty2;
        //public double DoubleProperty2
        //{
        //    get { return doubleProperty2; }
        //    set
        //    {
        //        double oldValue = doubleProperty2;
        //        double newValue = value;

        //        if (oldValue != newValue)
        //        {
        //            // Snap to 0.0 if moving in the direction of 0.0 and close enough.
        //            bool movingTowardsZero = (newValue > 0.0 && (newValue - oldValue) <= 0.0) || (newValue < 0.0 && (newValue - oldValue) >= 0.0);
        //            if (movingTowardsZero && Math.Abs(newValue) < 1)
        //            {
        //                doubleProperty2 = 0.0;
        //                NotifyPropertyChanged("DoubleProperty2");
        //            }

        //            else
        //            {
        //                doubleProperty2 = newValue;
        //                NotifyPropertyChanged("DoubleProperty2");
        //            }
        //        }
        //    }
        //}


        //double property3 = 0;
        //public double Property3
        //{
        //    get { return property3; }
        //    set
        //    {
        //        if (value != property3)
        //        {
        //            property3 = value;
        //            Property4 = property3;
        //            NotifyPropertyChanged("Property3");
        //        }
        //    }
        //}

        //double property4;
        //public double Property4
        //{
        //    get { return property4; }
        //    set
        //    {
        //        if (value != property4)
        //        {
        //            property4 = value;
        //            property3 = property4;
        //            NotifyPropertyChanged("Property3");
        //            NotifyPropertyChanged("Property4");

        //        }
        //    }
        //}

    }

    //public class TextToDoubleValidationRule : ValidationRule
    //{

    //    double d;
    //    public TextToDoubleValidationRule()
    //    {
    //        Minimum = double.MinValue;
    //        Maximum = double.MaxValue;
    //    }

    //    public double Minimum { get; set; }
    //    public double Maximum { get; set; }
    //    public string ErrorMessage { get; set; }
    //    /*public double Value {
    //       get {return correctedValue;}
    //        set{
    //            if (Minimum <= correctedValue && correctedValue <= Maximum)
    //            {
    //                Value = correctedValue;
    //            }
    //            else {
    //                Value = Math.Min(Math.Max(Minimum, value), Maximum);
    //            }
    //        }
    //    }*/

    //    //public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    //    //{
    //    //    string inputString = (value ?? string.Empty).ToString();

    //    //    if (!string.IsNullOrWhiteSpace(inputString))
    //    //    {

    //    //        NumberStyles style = NumberStyles.Number;
    //    //        if (double.TryParse(inputString, style, cultureInfo, out d))
    //    //        {
    //    //            if (Minimum <= d && d <= Maximum)
    //    //                return new ValidationResult(true, null);
    //    //        }
    //    //    }
    //    //    return new ValidationResult(false, this.ErrorMessage);
    //    //}

    //    //private void OnSomeTextBoxValidationError(object sender, ValidationErrorEventArgs e) {

    //    //    if (d > Maximum || d < Minimum) {
    //    //     //   ValidateValueCallback.Equals(Math.Min(Math.Max(Minimum, d), Maximum));

    //    //    }
    //    //}
    //}





    [ValueConversion(typeof(double), typeof(double))]
    public class SliderDoubleConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!string.IsNullOrEmpty(value as string) && parameter != null)
            {
                string parameterFromWPF = parameter as string;
                var controlData = parameterFromWPF.Split((";").ToCharArray());
                double val = value.GetType() == typeof(double) ? (double)value : System.Convert.ToDouble(value, culture);
                if (controlData[0].ToLower() == "border")
                {
                    double min = System.Convert.ToDouble(controlData[1], culture);
                    double max = System.Convert.ToDouble(controlData[2], culture);
                    FrameworkElement framework = new FrameworkElement();
                    var brush = framework.TryFindResource("myBorderBrush");
                   // return val <= max && val >= min ? Brushes.Black: Brushes.Red;
                    return val <= max && val >= min ? brush : Brushes.Red;
                }
                else
                    return null;
            }
            else
                return value;
        }


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && parameter != null)
            {
                string parameterFromWPF = parameter as string;
                var controlData = parameterFromWPF.Split((";").ToCharArray());
                string controlType = controlData[0];
                double val = value.GetType() == typeof(double) ? (double)value : System.Convert.ToDouble(value, culture);

                if (controlType.ToLower() == ("slider"))
                {
                    double p = System.Convert.ToDouble(controlData[1], culture);
                    if (Math.Abs(val) > p)
                    {
                        return val;
                    }
                    else
                    {
                        return 0.0;
                    }
                }

                else if (controlType.ToLower() == "textbox")
                {
                    double min = System.Convert.ToDouble(controlData[1], culture);
                    double max = System.Convert.ToDouble(controlData[2], culture);
                    return Math.Min(max, Math.Max(min, val));
                }
                /* else if (controlType.ToLower() == "border")
                 {
                     double min = System.Convert.ToDouble(controlData[1], culture);
                     double max = System.Convert.ToDouble(controlData[2], culture);
                     return val <= max && val >= min ? Brushes.Black : Brushes.Red;
                 }*/
                else return null;
            }
            else
                return null;
        }

    }
}
        //public class theTextBox:TextBox {    

        //protected override void OnLostFocus(RoutedEventArgs e)
        //{
        //    if (Validation.GetHasError(this))
        //    {
        //        var expression = GetBindingExpression(TextBox.TextProperty);
        //        if (expression != null)
        //            expression.UpdateTarget();
        //    }
        //    base.OnLostFocus(e);
        //}   
    //}


        //public class TextBoxBorderBrushConverter : IValueConverter
        //{
      
        //    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        //    {
        //       if (!string.IsNullOrEmpty(value as string) && parameter != null)
        //        {
        //            string parameterFromWPF = parameter as string;
        //            var controlData = parameterFromWPF.Split((";").ToCharArray());
        //            double val = value.GetType() == typeof(double) ? (double)value : System.Convert.ToDouble(value, culture);
        //            double min = System.Convert.ToDouble(controlData[0], culture);
        //            double max = System.Convert.ToDouble(controlData[1], culture);
        //            return val <= max && val >= min ? Brushes.Black : Brushes.Red;
        //        }
        //        else return Brushes.Red;
        //    }

        //    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        //    {

        //        return value;
        //    }
        //}
//}

 